﻿using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace QRDemo
{

	public class ArgsSign
	{
		/// <summary>
		/// AES base64加密
		/// </summary>
		/// <returns>The encrypt data.</returns>
		/// <param name="keyStr">Key string.</param>
		/// <param name="data">Data.</param>
		public string getEncryptData(string keyStr, string data) 
		{
			try{//"07effaba06ccb18d5a300e383f36a9a0120001"  "sXoNRVaFwUn62GZu"
				return XYEncryptionTool.base64(XYEncryptionTool.AESEncrypt(data, keyStr));
			} catch (Exception e) {
				throw e;
			};
		}



		public static string getSineValue(string content)
		{
			string result = "";

			StreamReader reader = new StreamReader(@"/Users/ryeoxy/Projects/QRDemo/QRDemo/Properties/cer/test2_rsa_private_key.pem");
			string privatekey = reader.ReadToEnd();
			reader.Close();

			RSACryptoServiceProvider rsa = test2PrivateKey(privatekey);
			result = Convert.ToBase64String(rsa.Encrypt(Encoding.UTF8.GetBytes(content), false));
			return result;
		}


		private static RSACryptoServiceProvider test2PrivateKey(string rsaKeyStr)
		{
			rsaKeyStr = @"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDomLfMwp4x2L4A
I+rlSh/g8P0kHgwKbJaN/unroSF47jlFha/EgyHUQm8IYG5bVZ3XlfPJJAXwWi7n
ry9XfxT5iaTIiyhX4Xf68BJTw5TjrP+4wy1NQZkqDS/vUmbkXmWnWyWmukjpuTe4
zxHUla9IINndLkS7LFY94cDnsQV8fxUFwylemycn4IrVqFuO80WpRzbeDNo3KtUI
m+3n88gYX/2ma2stgfeVfPGW6QiVGhxs3pS+O79oejkNUbcfl+gDujH430wiu61j
xCESGwK+XMzAnz9sEa2fjE/TwvvHgRTIhEssc6/savnPkhPtm9LgiOOvBrjFs118
k7tTycCzAgMBAAECggEBANDUDHEyaoWmazAFD2g9XNHAjAZZU8YoKCPksnBzgnHo
sFLS7DQq57yCyN1kdrBZ9hUQ9IeqaAqbbXFlv8N2vYYtJl1HUzlTnT0XPd0mxA6F
zDI+SfYK51D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D77xXu942w7HnRIk
Hgx5yZS92zh2QIW8famlnxte";
			//rsaKeyStr = rsaKeyStr.Replace("-----BEGIN PRIVATE KEY-----", "")
			//					 .Replace("-----END PRIVATE KEY-----","");
			return DecodeRSAPrivateKey(rsaKeyStr);
		}


		private static RSACryptoServiceProvider DecodeRSAPrivateKey(string privateKey)
		{
			//privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDomLfMwp4x2L4A\nI+rlSh/g8P0kHgwKbJaN/unroSF47jlFha/EgyHUQm8IYG5bVZ3XlfPJJAXwWi7n\nry9XfxT5iaTIiyhX4Xf68BJTw5TjrP+4wy1NQZkqDS/vUmbkXmWnWyWmukjpuTe4\nzxHUla9IINndLkS7LFY94cDnsQV8fxUFwylemycn4IrVqFuO80WpRzbeDNo3KtUI\nm+3n88gYX/2ma2stgfeVfPGW6QiVGhxs3pS+O79oejkNUbcfl+gDujH430wiu61j\nxCESGwK+XMzAnz9sEa2fjE/TwvvHgRTIhEssc6/savnPkhPtm9LgiOOvBrjFs118\nk7tTycCzAgMBAAECggEBANDUDHEyaoWmazAFD2g9XNHAjAZZU8YoKCPksnBzgnHo\nsFLS7DQq57yCyN1kdrBZ9hUQ9IeqaAqbbXFlv8N2vYYtJl1HUzlTnT0XPd0mxA6F\nzDI+SfYK51Dx9g+QlACFIA9Zrjw7FCuDt5udjgynN3AyzmfemqbR77wva9qY/KwA\nVr5OXK/4FBaunHsi6U3dsfl9s4r+BCp/tWdU12xkBr0nIeQo19VrseUyiHd7uaxF\nRav2EdzNVm79aRwnaQ0znGhoSQJxZm+UqKikaKwIgW/cYqHaFltKsx2LVx1+cPvZ\neCwWEv9C6joW5IQd1knGp7bkWT9MKngKXWDS4Yg3A0ECgYEA+ahs9fpf5zyg1CYW\nYQG0f+U3wOufgCsOPh1bX8uP3fCdiJITWGfpm9+yP1RUi/wvJE1EOcKuOIquaS82\nIYFs1+tnXNAYNu2zOJ+FUFTUKfigbwcZlQDJIhrHNxDZAfl/g2xWhMtF79CNYoBO\n/li0QDKscFVu+8inoVnvAG7PyocCgYEA7oFWx4jB3NS3rVC50APeiK82fc9nwGkH\nC31BL+KP5WqhtXTrZA1Qw8blrmwwqEd5zj8ER8HQD/MLyRLWQzYcrk6IAqyLLW2U\naTj/kROpJpAS9AaY8oyNtkJ4gLBxRjqyBJ09RIdwm1TofdF/Lyp6+JFWToJTyesH\nrH5ScBUTh3UCgYAua+zmyAlxG8rj57kdjI2dgmedjbJ4yIi7cAX/iABWSllgfZjv\nRVMg0rV4JRwbc2bonx1iiBqRFOhdUpQEIua0N2de0KrP+97pVnAZgcx5iWt7eZx+\nIklD+Cq2yIExZs0ZCNoFky6L6wF/IVCO5ruNfF6n/JrIYoUY8XoJYJFunQKBgHVW\nKE1OGKH9/MdFecS+wpJYMOTATvi4MwESBCn8unJRfXD6LnwwmNVYWtLu4xlG4MP4\n+LAcx7GxDtFDdSnny0Bt3jgCjw4wZN1Hb2P+Wi9gnhJz1HcvcuCB3yrgozm70qy2\ndtfqEho5MZAEqx+XNJxpVB6Xu4URHdtc4k3F8K+pAoGBAK0bhie3ykVJX+HhAnkt\nw+A78RSORdj44szlu2HPDrojGjtCY9NwkyR6FX6NkXkq1rS8H0oUMZtselxNRxzR\nWK+gsIQfA89PN/8hnOiCcgWt9CZdsdOcB6oLP+57I/icI9gGD77xXu942w7HnRIk\nHgx5yZS92zh2QIW8famlnxte";

			byte[] privateKeyBits = System.Convert.FromBase64String(privateKey);

			var RSA = new RSACryptoServiceProvider();
			var RSAparams = new RSAParameters();

			//using (BinaryReader binr = new BinaryReader(new MemoryStream(privateKeyBits)))
			//{
				BinaryReader binr = new BinaryReader(new MemoryStream(privateKeyBits));
				byte bt = 0;
				ushort twobytes = 0;
				twobytes = binr.ReadUInt16();
				if (twobytes == 0x8130)
					binr.ReadByte();
				else if (twobytes == 0x8230)
					binr.ReadInt16();
				else
					throw new Exception("Unexpected value read binr.ReadUInt16()");

				twobytes = binr.ReadUInt16();
				if (twobytes != 0x0102)
					throw new Exception("Unexpected version");

				bt = binr.ReadByte();
				if (bt != 0x00)
					throw new Exception("Unexpected value read binr.ReadByte()");

				RSAparams.Modulus = binr.ReadBytes(GetIntegerSize(binr));
				RSAparams.Exponent = binr.ReadBytes(GetIntegerSize(binr));
				RSAparams.D = binr.ReadBytes(GetIntegerSize(binr));
				RSAparams.P = binr.ReadBytes(GetIntegerSize(binr));
				RSAparams.Q = binr.ReadBytes(GetIntegerSize(binr));
				RSAparams.DP = binr.ReadBytes(GetIntegerSize(binr));
				RSAparams.DQ = binr.ReadBytes(GetIntegerSize(binr));
				RSAparams.InverseQ = binr.ReadBytes(GetIntegerSize(binr));
			//}

			RSA.ImportParameters(RSAparams);
			return RSA;
		}

		private static int GetIntegerSize(BinaryReader binr)
		{
			byte bt = 0;
			byte lowbyte = 0x00;
			byte highbyte = 0x00;
			int count = 0;
			bt = binr.ReadByte();
			if (bt != 0x02)
				return 0;
			bt = binr.ReadByte();

			if (bt == 0x81)
				count = binr.ReadByte();
			else
				if (bt == 0x82)
			{
				highbyte = binr.ReadByte();
				lowbyte = binr.ReadByte();
				byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
				count = BitConverter.ToInt32(modint, 0);
			}
			else
			{
				count = bt;
			}

			while (binr.ReadByte() == 0x00)
			{
				count -= 1;
			}
			binr.BaseStream.Seek(-1, SeekOrigin.Current);
			return count;
		}
	}

}
